<?php
namespace app\common\model;

// use think\Db;
// Db::listen(function($sql, $time, $explain){
//     // 记录SQL
//     echo $sql. ' ['.$time.'s]';
//     // 查看性能分析结果
//     dump($explain);
// });
// use think\Model;
class Swiper{
	private $user;
	private $user_id;
	private $config;
	private $isMarketingAdmin;
	private $state_removed = 0;
	private $state_inactive = 1;
	private $state_active = 2;
	private $cache;
	function __construct(){
		$user = $this->user = model('user');
		$user_id = $this->user_id = $user->getUserId();
		$admin = model('admin')->is('marketing');
		$this->cache = config('name').'marketing.swiper.list';
		$this->isMarketingAdmin = isset($admin['state'])?$admin['state']:0;
	}
	function add($url=null){
		if(!$url) return['state'=>0,'errormsg'=>'url empty'];
		if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
		if(!$this->isMarketingAdmin) return ['state'=>0,'errormsg'=>'authority incorrect'];
		$find = db('swiper')->where(['url'=>$url])->find();
		if($find){
			return ['state'=>0,'errormsg'=>'url exist'];
		}
		$insert = ['admin_user_id'=>$this->user_id,'create_time'=>time(),'url'=>$url,'state'=>$this->state_inactive];
		$swiper_id = db('swiper')->insertGetId($insert);
		if(!$swiper_id){
			return ['state'=>0,'errormsg'=>'swiper insert error'];
		}
		return ['state'=>1,'swiper_id'=>$swiper_id];
	}
	function remove($swiper_id){
		if(!$swiper_id) return['state'=>0,'errormsg'=>'swiper_id empty'];
		if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
		if(!$this->isMarketingAdmin) return ['state'=>0,'errormsg'=>'authority incorrect'];
		$update = ['state'=>$this->state_removed];
		$where = ['swiper_id'=>$swiper_id];
		db('swiper')->where($where)->update($update);
		cache($this->cache,null);
		return ['state'=>1];
	}
	function remove_all(){
		if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
		if(!$this->isMarketingAdmin) return ['state'=>0,'errormsg'=>'authority incorrect'];
		$update = ['state'=>$this->state_removed];
		$where = ['state'=>$this->state_inactive];
		db('swiper')->where($where)->update($update);
		cache($this->cache,null);
		return ['state'=>1];
	}
	function get_list(){
		if(cache($this->cache)){
			return ['state'=>1,'data'=>['rank'=>cache($this->cache)]];
		}
		$where = ['state'=>$this->state_active];
		$order = ['`range` asc, create_time desc'];
		$field = ['create_time','url','text'];
		$res = db('swiper')
			->where($where)
			->order($order)
			->field($field)
			->select();
		foreach ($res as $key => $value) {
			$text = json_decode($value['text'],1);
			if($text){
				foreach( $text as $k => $v) {
					$res[$key][$k] = $v;
				}
			}
			unset($res[$key]['text']);
			$res[$key]['create_time'] = timeString($value['create_time']);
		}
		$rank = $res;
		cache($this->cache,$rank);
		return ['state'=>1,'data'=>['rank'=>$rank]];
	}
	function get_list_admin(){
		if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
		if(!$this->isMarketingAdmin) return ['state'=>0,'errormsg'=>'authority incorrect'];
		$where = ['state'=>$this->state_active];
		$order = ['`range` asc,create_time asc'];
		$field = ['swiper_id','create_time','modify_time','url','text','admin_user_id','modify_user_id'];
		$res = db('swiper')
			->where($where)
			->order($order)
			->field($field)
			->select();
		foreach ($res as $key => $value) {
			$user_ids[] = $value['admin_user_id'];
			if($value['modify_user_id']) $user_ids[] = $value['modify_user_id'];
			$text = json_decode($value['text'],1);
			if($text){
				foreach( $text as $k => $v) {
					$res[$key][$k] = $v;
				}
			}
			unset($res[$key]['text']);
			$res[$key]['create_time'] = timeString($value['create_time']);
			$res[$key]['modify_time'] = timeString($value['modify_time']);
		}
		if(isset($user_ids)){
			$user_ids = array_unique($user_ids);
			$userInfo = $this->user->getInfoById($user_ids);
			foreach ($res as $key => $value) {
				$res[$key]['admin_user_name'] = $userInfo[$value['admin_user_id']]['set_name']?$userInfo[$value['admin_user_id']]['set_name']:$userInfo[$value['admin_user_id']]['user_name'];
				if($value['modify_user_id']) $res[$key]['modify_user_name'] = $userInfo[$value['modify_user_id']]['set_name']?$userInfo[$value['modify_user_id']]['set_name']:$userInfo[$value['modify_user_id']]['user_name'];
			}
		}
		$rank = $res;
		return ['state'=>1,'data'=>['rank'=>$rank]];
	}
	function get_list_inactive(){
		if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
		if(!$this->isMarketingAdmin) return ['state'=>0,'errormsg'=>'authority incorrect'];
		$where = ['state'=>$this->state_inactive];
		$order = ['`range` asc,create_time asc'];
		$field = ['swiper_id','create_time','modify_time','url','text','admin_user_id','modify_user_id'];
		$res = db('swiper')
			->where($where)
			->order($order)
			->field($field)
			->select();
		foreach ($res as $key => $value) {
			$user_ids[] = $value['admin_user_id'];
			if($value['modify_user_id']) $user_ids[] = $value['modify_user_id'];
			$text = json_decode($value['text'],1);
			if($text){
				foreach( $text as $k => $v) {
					$res[$key][$k] = $v;
				}
			}
			unset($res[$key]['text']);
			$res[$key]['create_time'] = timeString($value['create_time']);
			$res[$key]['modify_time'] = timeString($value['modify_time']);
		}
		if(isset($user_ids)){
			$user_ids = array_unique($user_ids);
			$userInfo = $this->user->getInfoById($user_ids);
			foreach ($res as $key => $value) {
				$res[$key]['admin_user_name'] = $userInfo[$value['admin_user_id']]['set_name']?$userInfo[$value['admin_user_id']]['set_name']:$userInfo[$value['admin_user_id']]['user_name'];
				if($value['modify_user_id']) $res[$key]['modify_user_name'] = $userInfo[$value['modify_user_id']]['set_name']?$userInfo[$value['modify_user_id']]['set_name']:$userInfo[$value['modify_user_id']]['user_name'];
			}
		}
		$rank = $res;
		return ['state'=>1,'data'=>['rank'=>$rank]];
	}
	function range($ranges){
		if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
		if(!$this->isMarketingAdmin) return ['state'=>0,'errormsg'=>'authority incorrect'];
		if(!$ranges) return ['state'=>0,'range error'];
		$ranges = explode(",",$ranges);
		foreach ( $ranges as $range => $swiper_id) {
			$where = ['swiper_id'=>$swiper_id,'state'=>['<>',$this->state_removed]];
			$update = ['range'=>$range];

			db('swiper')->where($where)->update($update);
		}
		cache($this->cache,null);
		return ['state'=>1];
	}
	function release($swiper_id,$release){
		if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
		if(!$this->isMarketingAdmin) return ['state'=>0,'errormsg'=>'authority incorrect'];
		$where = ['swiper_id'=>$swiper_id,'state'=>['<>',$this->state_removed]];
		$update = ['state'=>$release?$this->state_active:$this->state_inactive];
		$res = db('swiper')->where($where)->update($update);
		cache($this->cache,null);
		return ['state'=>1];
	}
	function modify($swiper_id,$modify){
		if(!$swiper_id) return['state'=>0,'errormsg'=>'swiper_id empty'];
		if(!$modify) return ['state'=>0,'errormsg'=>'modify value error'];
		if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
		if(!$this->isMarketingAdmin) return ['state'=>0,'errormsg'=>'authority incorrect'];
		$where = ['swiper_id'=>$swiper_id,'state'=>['<>',$this->state_removed]];
		$update = ['text'=>$modify];
		$res = db('swiper')->where($where)->update($update);
		if(!$res){
			return ['state'=>0,'errormsg'=>'database save error'];
		}
		cache($this->cache,null);
		return ['state'=>1];

	}

    //左侧导航
    public function navigation_create()
    {
        if(!$this->user_id) return array('state'=>0,'errormsg'=>'请登录');
        $data['create_time'] = time();
        $data['state'] = '2';
        $data['user_id'] = $this->user_id;
        $result = db('navigation')->insertGetId($data);
        if (!$result) {
            return array('state' => 0, 'errormsg' => 'insert fail');
        }
//        加入action操作
        $action=new Action;
        $action->add('navigation_create', 'navigation_id', $result);
        return array('state' => 1, 'errormsg' => 'ok', 'errorcode' => '',);
    }
    //导航管理(编辑)
    public function navigation_modify($navigation_id, $key, $modifyinfo)
    {
        //id检查
        if (!($navigation_id > 0)) {
            return array('state' => 0, 'errormsg' => 'navigation id incorrect', 'errorcode' => '');
        }
        $data[$key] = $modifyinfo;
        $navigation_id = addslashes($navigation_id); //sql防注入
        $result = db('navigation')->where("navigation_id = $navigation_id")->update($data);
        // 记录action表动作行为
        if ($result) {
            foreach ($data as $key => $value) {
                model('action')->add('navigation_modify', $key, $value);
            }
            $ret = array('state' => 1 ,'errormsg'=>'ok');
        } else {
            $ret = array('state' => 0, 'errormsg' => 'modify error');
        }
        return $ret;
    }
    //开放关闭导航栏
    public function navigation_release($navigation_id,$release)
    {
        if (!$navigation_id > 0) {
            return array('state' => 0, 'errormsg' => 'brand_id incorrect', 'errorcode' => '');
        }
        if ($release == 1) {
            $data['state'] = 1;
        } else {
            $data['state'] = 2;
        }
        $where = "navigation_id in($navigation_id)";
        $typeconfigstate = db('navigation')->where($where)->update($data);
        if ($typeconfigstate) {
            return array('state' => 1, 'errormsg' => 'ok', 'errorcode' => '');
        }
        model('action')->add('navigation_release','state',$release);
        return array('state' => 0, 'errormsg' => 'save error', 'errorcode' => '');
    }
    //导航删除
    public function navigation_remove($navigation_id)
    {
        if(!$this->user_id) return array('state'=>0,'errormsg'=>'请登录');
        if(!$navigation_id) return array('state'=>0,'errormsg'=>'请选择一个导航栏');
        $res = db('navigation')->where("navigation_id in($navigation_id)")->update(['state'=>0]);
        // 记录action表动作行为
        if ($res) {
            model('action')->add('navigation_remove', 'navigation_id', $navigation_id);
            $ret = array('state' => 1 ,'errormsg'=>'ok');
        } else {
            $ret =array('state'=>0,'errormsg'=>'删除失败');
        }
        return $ret;
    }
    //导航栏列表
    function navigation_list(){
        $where = ['state'=>1];
        $order = ['`range` asc, create_time desc'];
        $field = ['create_time','cn'];
        $res = db('navigation')
            ->where($where)
            ->order($order)
            ->field($field)
            ->select();
        $brand = db('navigation_brand')->where('state = 1')->select();
        foreach ($res as $k =>$v){
            foreach ($brand as $kk => $vv){
                if($v['navigation_id'] == $vv['navigation_id']){
                    $res[$k]['brand'][] = $vv;
                }
            }
        }
        $rank = $res;
        return ['state'=>1,'data'=>['rank'=>$rank]];
    }
    function navigation_list_admin(){
        $order = ['`range` asc, create_time desc'];
        $field = ['create_time','cn'];
        $res = db('navigation')
            ->where('state !=0')
            ->order($order)
            ->field($field)
            ->select();
        $brand = db('navigation_brand')->where('state != 0')->select();
        foreach ($res as $k =>$v){
            foreach ($brand as $kk => $vv){
                if($v['navigation_id'] == $vv['navigation_id']){
                    $res[$k]['brand'][] = $vv;
                }
            }
        }
        $rank = $res;
        return ['state'=>1,'data'=>['rank'=>$rank]];
    }
    function navigation_range($ranges){
        if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
        if(!$this->isMarketingAdmin) return ['state'=>0,'errormsg'=>'authority incorrect'];
        if(!$ranges) return ['state'=>0,'range error'];
        $ranges = explode(",",$ranges);
        foreach ( $ranges as $range => $navigation_id) {
            $where = ['navigation_id'=>$navigation_id,'state'=>['<>',0]];
            $update = ['range'=>$range];

            db('navigation')->where($where)->update($update);
        }
        cache($this->cache,null);
        return ['state'=>1];
    }


    //导航栏商品
    public function navigation_brand_create()
    {
        if(!$this->user_id) return array('state'=>0,'errormsg'=>'请登录');
        $data['create_time'] = time();
        $data['state'] = '2';
        $data['user_id'] = $this->user_id;
        $result = db('navigation_brand')->insertGetId($data);
        if (!$result) {
            return array('state' => 0, 'errormsg' => 'insert fail');
        }
//        加入action操作
        $action=new Action;
        $action->add('navigation_brand_create', 'navigation_id', $result);
        return array('state' => 1, 'errormsg' => 'ok', 'errorcode' => '',);
    }
    //导航管理(编辑)
    public function navigation_brand_modify($navigation_brand_id, $key, $modifyinfo)
    {
        //id检查
        if (!($navigation_brand_id > 0)) {
            return array('state' => 0, 'errormsg' => 'navigation id incorrect', 'errorcode' => '');
        }
        $data[$key] = $modifyinfo;
        $navigation_id = addslashes($navigation_brand_id); //sql防注入
        $result = db('navigation_brand')->where("navigation_brand_id = $navigation_brand_id")->update($data);
        // 记录action表动作行为
        if ($result) {
            foreach ($data as $key => $value) {
                model('action')->add('navigation_brand_modify', $key, $value);
            }
            $ret = array('state' => 1 ,'errormsg'=>'ok');
        } else {
            $ret = array('state' => 0, 'errormsg' => 'modify error');
        }
        return $ret;
    }
    //开放关闭导航栏
    public function navigation_brand_release($navigation_brand_id,$release)
    {
        if (!$navigation_brand_id > 0) {
            return array('state' => 0, 'errormsg' => 'brand_id incorrect', 'errorcode' => '');
        }
        if ($release == 1) {
            $data['state'] = 1;
        } else {
            $data['state'] = 2;
        }
        $where = "navigation_brand_id in($navigation_brand_id)";
        $typeconfigstate = db('navigation_brand')->where($where)->update($data);
        if ($typeconfigstate) {
            return array('state' => 1, 'errormsg' => 'ok', 'errorcode' => '');
        }
        model('action')->add('navigation_brand_release','state',$release);
        return array('state' => 0, 'errormsg' => 'save error', 'errorcode' => '');
    }
    //导航删除
    public function navigation_brand_remove($navigation_brand_id)
    {
        if(!$this->user_id) return array('state'=>0,'errormsg'=>'请登录');
        if(!$navigation_brand_id) return array('state'=>0,'errormsg'=>'请选择一个导航栏');
        $res = db('navigation_brand')->where("navigation_brand_id in($navigation_brand_id)")->update(['state'=>0]);
        // 记录action表动作行为
        if ($res) {
            model('action')->add('navigation_brand_remove', 'navigation_brand_id', $navigation_brand_id);
            $ret = array('state' => 1 ,'errormsg'=>'ok');
        } else {
            $ret =array('state'=>0,'errormsg'=>'删除失败');
        }
        return $ret;
    }
    //导航栏商品列表
    function navigation_brand_list($filter){
        $where = 'state = 1';
        if($filter['navigation_id']){
            $where .= "navigation_id = $filter[navigation_id]";
        }
        $brand = db('navigation_brand')->where($where)->select();
        return ['state'=>1,'data'=>['rank'=>$brand]];
    }
    function navigation_brand_list_admin($filter){
        $where = 'state != 0';
        if($filter['navigation_id']){
            $where .= "navigation_id = $filter[navigation_id]";
        }
        $brand = db('navigation_brand')->where($where)->select();
        return ['state'=>1,'data'=>['rank'=>$brand]];
    }
    function navigation_brand_range($ranges){
        if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
        if(!$this->isMarketingAdmin) return ['state'=>0,'errormsg'=>'authority incorrect'];
        if(!$ranges) return ['state'=>0,'range error'];
        $ranges = explode(",",$ranges);
        foreach ( $ranges as $range => $navigation_brand_id) {
            $where = ['navigation_brand_id'=>$navigation_brand_id,'state'=>['<>',0]];
            $update = ['range'=>$range];

            db('navigation_brand')->where($where)->update($update);
        }
        cache($this->cache,null);
        return ['state'=>1];
    }


}